//
//  LXComposeToolbar.swift
//  weibo
//
//  Created by lixin on 16/4/23.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

enum LXComposeToolbarType: Int {
    
    //图片
    case Picture = 1
    
    //@
    case Mention = 2
    
    //#
    case Trend = 3
    
    //表情
    case Emoticon = 4
    
    //add
    case Add = 5
    
}

class LXComposeToolbar: UIView {
    
    //根据情况设置表情/键盘按钮的图标
    var isEmoticon:Bool = false{
        
        didSet{
            
            if isEmoticon{
                
                emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                
                emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{
                
                emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                
                emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
    }
    
    var emoticonBtn: UIButton?
    
    var composeToolBarColsure: ((type: LXComposeToolbarType)->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 视图设置
    private func setUI(){
        
        self.backgroundColor = UIColor.whiteColor()
        
        let pictureButton = addChildBtn("compose_toolbar_picture", type: .Picture)
        
        let mentionButton = addChildBtn("compose_mentionbutton_background", type: .Mention)
        
        let trendButton = addChildBtn("compose_trendbutton_background", type: .Trend)
        
        emoticonBtn = addChildBtn("compose_emoticonbutton_background", type: .Emoticon)
        
        let addButton = addChildBtn("compose_add_background", type: .Add)
        
        //添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.bottom.top.equalTo(self)
            
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(trendButton)
            
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(emoticonBtn!)
            
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        
        emoticonBtn!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            
            make.width.equalTo(addButton)
            
            make.leading.equalTo(trendButton.snp_trailing)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            
            make.leading.equalTo(emoticonBtn!.snp_trailing)
        }
    }
    
    //MARK: - 添加按钮的公共方法
    func addChildBtn(imgName: String, type: LXComposeToolbarType) -> UIButton{
        
        let button = UIButton()
        
        button.tag = type.rawValue
        
        button.addTarget(self, action: #selector(LXComposeToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
        return button
        
    }
    
    // MARK: -  点击事件
    @objc private func buttonClick(button: UIButton){
        
        composeToolBarColsure?(type: LXComposeToolbarType(rawValue: button.tag)!)
    }


}
